package fr.coupedumonde2014.app.authentification;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.plus.PlusClient;

/**
 * Created by dev-w8-gfi on 28/05/2014.
 */
public class GPlus implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

    private static final String TAG = "GOOGLE PLUS";
    private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

    private ProgressDialog mConnectionProgressDialog;
    private PlusClient mPlusClient;
    private ConnectionResult mConnectionResult;

    Activity activity;

    public interface GPlusDelegate{
        public void onConnected(PlusClient client);
    }

    GPlusDelegate delegate;

    public GPlus(Activity activity, GPlusDelegate delegate) {
        this.activity = activity;
        this.delegate = delegate;

        mPlusClient = new PlusClient.Builder(activity, this, this)
                .setVisibleActivities("http://schemas.google.com/AddActivity",
                        "http://schemas.google.com/BuyActivity")
                .build();
        // Barre de progression à afficher si l'échec de connexion n'est pas résolu.
        mConnectionProgressDialog = new ProgressDialog(activity);
        mConnectionProgressDialog.setMessage("Signing in...");
    }

    public void connect() {
        mPlusClient.connect();
    }

    public void disconnect() {
        mPlusClient.clearDefaultAccount();
        mPlusClient.disconnect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(activity, REQUEST_CODE_RESOLVE_ERR);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
                disconnect();
                connect();
            }
        }
        // Enregistrer le résultat et résoudre l'échec de connexion lorsque l'utilisateur clique.
        mConnectionResult = result;
    }

    public void onActivityResult(int requestCode, int responseCode, Intent intent) {
        if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == activity.RESULT_OK) {
            mConnectionResult = null;
            mPlusClient.connect();
        }
    }

    public void onConnected() {
        String accountName = mPlusClient.getAccountName();
        Toast.makeText(activity, accountName + " is connected.", Toast.LENGTH_LONG).show();
        Log.d(TAG,accountName + " is connected.");

        if(delegate != null && mPlusClient != null)
            delegate.onConnected(mPlusClient);
    }

    @Override
    public void onConnected(Bundle bundle) {
        onConnected();
    }

    @Override
    public void onDisconnected() {
        Log.d(TAG, "disconnected");
    }

}
